<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ZHOUYANG
 * Date: 13-3-30
 * Time: 下午2:37
 */
 
class CateController extends Controller{

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $cate_name = $this->input()->post('cate_name');
        $action = $this->input()->post('action');
        $filter = array();

        if($cate_name){
            $filter['cate_name'] = $cate_name;
        }

        if($action){
            $filter['action'] = $action;
        }

        $list = Model::getInstance('cate')->getCateList($curr_page,$per_page,$filter);
        $template['data'] = $list;
        Template::show($template);
    }

    public function tree(){
        
        $menu_arr = Model::getInstance('cate')->getTree();

        $template['data']['menu_arr'] = $menu_arr;
        $template['view'] = ($this->input()->get('type','tree')).'.php';//tree list

         Template::show($template);
    }

    public function add(){

        Template::show();
    }

    public function del(){

        $cate_id = $this->input()->get('cate_id');

        $cate_info = Model::getInstance('cate')->get($cate_id);

        if($cate_info['right_value'] - $cate_info['left_value'] > 1){

            $data = array(
                "statusCode"=>"300",
                "message"=>"该分类下有子分类，不能删除！",
            );
            echo Response::JSON($data);
            return false;
        }

        $ret = Model::getInstance('cate')->delAllCate($cate_id);

        if($ret){
            Model::getInstance('catehasrole')->delByCateId($cate_id);
            Model::getInstance('catehasuser')->delByCateId($cate_id);
            
            $child_node = Model::getInstance('cate')->getCateByLR($cate_info['left_value'],$cate_info['right_value']);

            foreach($child_node as $v){
                Model::getInstance('catehasrole')->delByCateId($v['cate_id']);
                Model::getInstance('catehasuser')->delByCateId($v['cate_id']);
            }

            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"cate",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){

        $cate_id = (int)$this->input()->get('cate_id');

        $cate_info = Model::getInstance('cate')->get($cate_id);

        if($cate_info['parent_cate_id']){

            $parent_cate_info = Model::getInstance('cate')->get($cate_info['parent_cate_id']);
            
        }else{

            $parent_cate_info = array(
                'cate_id'=>0,
                'cate_name'=>'管理中心'
            );
        }

        $roles = Model::getInstance('role')->getCateRoles($cate_id);

        $_role_ids = $_role_names = array();
        
        foreach($roles as $v){
            $_role_ids[] = $v['role_id'];
            $_role_names[] = $v['role_name'];
        }
        
        $data = array(
            'cate_info'=>$cate_info,
            'parent_cate_info'=>$parent_cate_info,
            'role_ids'=>implode(',',$_role_ids),
            'role_names'=>implode(',',$_role_names),
        );

        $template = array('data'=>$data);
        Template::show($template);
    }
}